package hirt;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Point;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.util.Vector;

/**
 * The popup window that implements the right clicking window.
 *
 * @author Tim Pruitt
 */
public class ScenePopupMenu extends PopupMenu implements ActionListener {

    /**
     * The local client.
     *
     * @author Tim Pruitt
     */
    protected HirtClient client;

    /**
     * The list of commands able to be performed thru the right-clicking window.
     *
     * @author Tim Pruitt
     */
    protected Vector<Integer> commandList;

    /**
     * The list of items adjoining the right-clicking window.
     *
     * @author Tim Pruitt
     */
    protected Vector<Integer> itemIndexList;

    /**
     * The constructor for the right-clicking window.
     *
     * @author Tim Pruitt
     *
     * @param client The local client.
     */
    public ScenePopupMenu(HirtClient client) {
        this.client = client;
        commandList = new Vector<Integer>();
        itemIndexList = new Vector<Integer>();
    }

    /**
     * Inserts a command and an item index into the right-click window.
     *
     * @author Tim Pruitt
     *
     * @param mi The menu item being added.
     * @param command The command associated with the specific task.
     * @param itemIndex The index of the item being associated with the command.
     */
    public MenuItem add(MenuItem mi, int command, int itemIndex)
    {
        commandList.add(command);
        itemIndexList.add(itemIndex);

        return super.add(mi);
    }

    /**
     * Inserts a command into the right-click window.
     *
     * @author Tim Pruitt
     *
     * @param mi The menu item being added to the window.
     * @param command The command associated with the specific task.
     */
    public MenuItem add(MenuItem mi, int command)
    {
        commandList.add(command);
        itemIndexList.add(-1);

        return super.add(mi);
    }

    @Override
    public MenuItem add(MenuItem mi)
    {
        MenuItem tempMI = super.add( mi );

        commandList.add(-2);
        itemIndexList.add(-2);

        return( tempMI );
    }

    /**
     * Implements the action of an item in the right-click window being selected.
     *
     * @author Tim Pruitt
     *
     * @param e The event of an item in the right-click window being selected.
     */
    public void actionPerformed(ActionEvent e) {
        /**
         * The object that was selected.
         * 
         * @author Tim Pruitt
         */
        Object testObject = e.getSource();

        /**
         * Gets the particular active menu item index.
         *
         * @author Tim Pruitt
         */
        int index;
        for( index = 0; index < getItemCount(); index++ )
        {

            if( getItem( index) == testObject )
            {
                break;
            }
        }

        /**
         * The local session's current mode.
         *
         * @author Tim Pruitt
         */
        int mode = client.getSession().getMode();

        // Get corresponding command/itemIndex.
        System.out.println( "actionPerformed(): size of commandList = " + commandList.size());

        /**
         * The command that was selected.
         *
         * @author Tim Pruitt
         */
        int command = commandList.get(index);

        /**
         * The item index that was selected.
         *
         * @author Tim Pruitt
         */
        int itemIndex = itemIndexList.get( index );

        System.out.println( "actionPerformed(): menu item selected = " + index);

        /**
         * Get's the local client's GUI.
         *
         * @author Tim Pruitt
         */
        MainScreenPanel mainScreenPanel = client.getGUI().mainScreenPanel;

        /**
         * The current scenario's world item.
         *
         * @author Tim Pruitt
         */
        Item2D world = client.getSession().getCurrentScenario().getWorld();

        /**
         * The item that was right-clicked.
         *
         * @author Tim Pruitt
         */
        Item2D item = mainScreenPanel.getRightClickedItem();

        /**
         * The local player's current vehicle.
         *
         * @author Tim Pruitt
         */
        Automobile currentCar = mainScreenPanel.getLocalPlayer().getCurrentCar();

        switch(command)
        {
            // Lights
            case RightClickCommands.LIGHTS_OFF:
                client.setLights(currentCar, ServerConstants.LIGHT_STATE_OFF);
                break;
            case RightClickCommands.LIGHTS_CAUTION:
                client.setLights(currentCar, ServerConstants.LIGHT_STATE_CAUTION);
                break;
            case RightClickCommands.LIGHTS_LEFT_ARROW:
                client.setLights(currentCar, ServerConstants.LIGHT_STATE_LEFT_ARROW);
                break;
            case RightClickCommands.LIGHTS_RIGHT_ARROW:
                client.setLights(currentCar, ServerConstants.LIGHT_STATE_RIGHT_ARROW);
                break;

            case RightClickCommands.INSERT_ITEM:

                String type = ServerConstants.ITEM_TYPE_NAME[itemIndex];

                if( mode == Session.MODE_EDITOR )
                {
                    client.sendCreateRequest(type, ServerConstants.ITEM_CLASS[itemIndex],
                            world.getId(),
                            mainScreenPanel.getRightClickX() - (ServerConstants.ITEM2D_DIMENSIONS[itemIndex].x / 2),
                            mainScreenPanel.getRightClickY() - (ServerConstants.ITEM2D_DIMENSIONS[itemIndex].y / 2));

                    System.out.println( "ScenePopup: Inserting Item " + type + "-" + ServerConstants.ITEM_CLASS[itemIndex]);
                }
                else if( mode == Session.MODE_PLAY )
                {
                    if( currentCar.getItemCountByType( type ) > 0 )
                    {
                        Item2D tempItem2D = ( Item2D )currentCar.getFirstItemByType( type );

                        //TODO: THIS for play mode as transfer/move.
                        client.moveItem( tempItem2D, currentCar.getPosition(),
                                new Point (mainScreenPanel.getRightClickX(), mainScreenPanel.getRightClickY()) );
                        client.sendTransfer(currentCar, tempItem2D, world);
                    }
                }
                break;

            case RightClickCommands.CHECK_VICTIM:
                break;

            case RightClickCommands.LEAVE_SCENE:
                client.sendRemove( item );
                break;
            case RightClickCommands.MOVE_VEHICLE_HERE:
                if ( !client.getSession().getCurrentScenario().detectCollision(currentCar) ) {
                    client.moveItem(currentCar, currentCar.getPosition(),
                            new Point(mainScreenPanel.getRightClickX(), mainScreenPanel.getRightClickY()) );
                }
                break;

            case RightClickCommands.PICK_UP_ITEM:
                client.sendTransfer( world, item, currentCar );
                break;

            case RightClickCommands.PLAY_ROLE:
                Automobile vehicle = (Automobile) item;
                client.addRole( vehicle.getRoleId() );
                break;

            case RightClickCommands.REMOVE:
                client.sendRemove(item);
                break;

            case RightClickCommands.REQUEST_RESPONDER_MOVE:
                client.sendRequest(client.getLocalPlayer(),
                        (Automobile) item,
                        CommunicationAction.TYPE_REQUESTMOVE);
                break;

            case RightClickCommands.REQUEST_VICTIM_MOVE:
                // Do not need anymore since we treat a victim as a responder
            break;

            // Vehicle Conditions
            case RightClickCommands.SET_TO_DAMAGED:
                client.setDamage(client.getLocalPlayer().getCurrentCar(), true);
                break;
            case RightClickCommands.SET_TO_MOVEABLE:
                client.setMovable(client.getLocalPlayer().getCurrentCar(), true);
                break;
            case RightClickCommands.SET_TO_NONDAMAGED:
                client.setDamage(client.getLocalPlayer().getCurrentCar(), false);
                break;
            case RightClickCommands.SET_TO_NONMOVEABLE:
                client.setMovable(client.getLocalPlayer().getCurrentCar(), false);
                break;
            case RightClickCommands.SET_TO_NO_FIRE:
                client.setOnFire(client.getLocalPlayer().getCurrentCar(), false);
                break;
            case RightClickCommands.SET_TO_ON_FIRE:
                client.setOnFire(client.getLocalPlayer().getCurrentCar(), true);
            break;
            
            case RightClickCommands.STOP_ROLE:
                Automobile tempVehicle = (Automobile) item;
                client.removeRole( tempVehicle.getRoleId() );
            break;
            case RightClickCommands.TOW_VEHICLE:
                client.sendTransfer(currentCar, item, currentCar);
                client.sendRemove(currentCar);
                break;
            case RightClickCommands.PUT_OUT_FIRE:
                client.setOnFire( (Automobile) item, false );
                break;
            case RightClickCommands.SWITCH_TO_VEHICLE:
                client.getLocalPlayer().setCurrentCar( (Automobile) item );
                client.getGUI().mainScreenPanel.refreshVehicle();
                break;
            default:
                System.err.println("ScenePopupMenu.actionPerformed( e ): " +
                        "could not find the command given.");
                break;
        }
    }
}
